<!DOCTYPE html>
<html lang="zh-CN" xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">
<head th:include="/common/inc::header"/>
<body>
<form class="layui-form">
    <input name="id" id="id" type="hidden"/>
    <br/>
    <div class="layui-form-item" style="margin-left: 10%">
        <label class="layui-form-label" style="width: 120px">设备名称</label>
        <div class="layui-input-inline">
            <input type="text" name="deviceName" id="deviceName" autocomplete="off" lay-verify="required"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item" style="margin-left: 10%">
        <label class="layui-form-label" style="width: 120px">设备编号</label>
        <div class="layui-input-inline">
            <input type="text" name="deviceId" id="deviceId" autocomplete="off" lay-verify="required"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item" style="margin-left: 10%">
        <label class="layui-form-label" style="width: 120px">设备IP地址</label>
        <div class="layui-input-inline">
            <input type="text" name="deviceIp" id="deviceIp" autocomplete="off" lay-verify="required"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item" style="margin-left: 10%">
        <label class="layui-form-label" style="width: 120px">所在考场</label>
        <div class="layui-input-inline">
            <select name="examineeHallId" id="examineeHallId" lay-filter="examineeHallId">
            </select>
        </div>
    </div>
    <button class="layui-btn" id="formSubmit" lay-submit="" lay-filter="formSubmit" style="display: none">立即提交
    </button>
</form>
<script th:replace="/common/inc::incJs"></script>
<script>
    var form = layui.form;
    var successCallback;
    var id = getQueryString("id");
    $("#id").val(id);


    //监听提交
    form.on('submit(formSubmit)', function (data) {
        data.field.examineeHallName = $("#examineeHallId").find("option:selected").text();
        if (id != "") {
            $.post('/backend/security/deviceInfo/updateDevice', data.field, function (res) {
                if (res.code < 0) {
                    layer.msg(res.message);
                } else {
                    layer.msg('操作成功');
                    successCallback && successCallback();
                }
            });
        } else {
            $.post('/backend/security/deviceInfo/addDevice', data.field, function (res) {
                if (res.code < 0) {
                    layer.msg(res.message);
                } else {
                    layer.msg('操作成功');
                    successCallback && successCallback();
                }
            });
        }
        return false;
    });

    var formSubmit = function (callback) {
        successCallback = callback;
        $("#formSubmit").click();
    }

    //初始化加载
    $(function () {
        $.post('/backend/security/examineeHall/getAllHall', function (res) {
            if (res.code < 0) {
                    layer.msg(res.message);
                } else {
                    $.each(res.body, function (i, item) {
                        $("#examineeHallId").append("<option value='" + item.id + "'>" + item.examineeHallName + "</option>");
                    });
                    form.render('select');

                if (id != "") {
                    $.post('/backend/security/deviceInfo/getById', {id: id}, function (res) {
                        if (res.code < 0) {
                            layer.msg(res.message);
                        } else {
                            $("form").loadData(res.body);
                            layui.form.render('select');
                        }
                    });
                }
            }
        });
    })
</script>
</body>
</html>
